/** 
 * Utils.java
 * @author Lex
 * 2007-9-8 ����01:18:58
 */
package org.lex.utils;

import java.util.Comparator;

public class Utils {
	public static int min(int... data) {
		if (null == data || 0 == data.length)
			throw new IllegalArgumentException("Arguments contains nothing!");
		int min = Integer.MAX_VALUE;
		for (int i : data)
			if (i < min)
				min = i;
		return min;
	}

	public static int max(int... data) {
		if (null == data || 0 == data.length)
			throw new IllegalArgumentException("Arguments contains nothing!");
		int max = Integer.MIN_VALUE;
		for (int i : data)
			if (i > max)
				max = i;
		return max;
	}

	public static <T extends Comparable<T>> T min(T... data) {
		return min(new CP<T>(), data);
	}

	public static <T extends Comparable<T>> T max(T... data) {
		return max(new CP<T>(), data);
	}

	public static <T> T min(Comparator<T> cp, T... data) {
		if (null == data || 0 == data.length)
			throw new IllegalArgumentException("Arguments contains nothing!");
		T min = data[0];
		for (int i = 0; i < data.length; i++) {
			T t = data[i];
			if (cp.compare(t, min) < 0)
				min = t;
		}
		return min;
	}

	public static <T> T max(Comparator<T> cp, T... data) {
		if (null == data || 0 == data.length)
			throw new IllegalArgumentException("Arguments contains nothing!");
		T max = data[0];
		for (int i = 0; i < data.length; i++) {
			T t = data[i];
			if (cp.compare(t, max) > 0)
				max = t;
		}
		return max;
	}

	static class CP<T extends Comparable<T>> implements Comparator<T> {
		public int compare(T o1, T o2) {
			return o1.compareTo(o2);
		}
	}
}
